//
//  EAEverydayChangeController.m
//  WhatToEat
//
//  Created by 方力 on 15/10/31.
//  Copyright © 2015年 Mac. All rights reserved.
//
#import "EAPinKerView.h"
#import "EAEverydayChangeController.h"
#import "EAMainController.h"
@interface EAEverydayChangeController ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property(strong,nonatomic)NSArray *pickerArray;

@property(strong,nonatomic)EAPinKerView * pinkerView;

@end
@implementation EAEverydayChangeController
//__weak typeof(self) weakSelf=self;
- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.navigationItem.title=@"每日一更";
    
       self.pinkerView=[self pinKerViews];

    
    



    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 44, 44);
    
    [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
        backBtn.imageEdgeInsets=UIEdgeInsetsMake(0, -22, 0, 22);
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backItem;
    
    
    
    UIButton * bt=[[UIButton alloc] init];

    [self.tableView addSubview:bt];

    [bt setTitle:@"下一步" forState:0];
    
    bt.backgroundColor=kTingColor;
    
    
    [bt mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.size.mas_equalTo(CGSizeMake(kUI_SCREEN_WIDTH-150, 34));
        
        
        make.centerX.mas_equalTo(self.tableView);
        
        
        make.top.mas_equalTo(self.tableView.mas_top).with.offset(kUI_SCREEN_WIDTH/3);
        
        
    }];
    
    

    self.tableView.showsHorizontalScrollIndicator=YES;
    self.tableView.showsVerticalScrollIndicator=YES;
    [bt addTarget:self action:@selector(okAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    UITapGestureRecognizer * tp=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelAllKey)];
    
    
    [self.tableView addGestureRecognizer:tp];
    
    
    [Commtools setExtraCellLineHidden:self.tableView];
    


}

-(void)viewWillAppear:(BOOL)animated
{

    [super viewWillAppear:animated];
    
    self.weightTF.text=[NSString stringWithFormat:@"%@kg",ApplicationDelegate.userDictionary[@"weight"]];
    self.heightTF.text=[NSString stringWithFormat:@"%@cm",ApplicationDelegate.userDictionary[@"height"]];
    
    self.tableView.height=kUI_SCREEN_HEIGHT;
    
    

}



-(void)backAction
{

    [self.navigationController popViewControllerAnimated:YES];
}


-(void)okAction
{


    
    
    [WebAPI webAPISignAddMd5tWithNote:@"user/profile" parm:@{
                                                             @"userID":ApplicationDelegate.userId,
                                                             
                                                             
                                                             
                                                             
                                                             
                                                             @"weight":self.weightTF.text,
                                                             
                                                             @"height":self.heightTF.text,
                                                             
                                                             
                                                             } completed:^(NSInteger state, AFHTTPRequestOperation *operation, id responseObject) {
        
        
        
        if ([responseObject[@"code"] isEqualToString:@"1"]) {
            
            
            [EASaveUserMessage saveUserMessage:responseObject andView:nil];
            

            
            
            
            EAMainController * mVC=[[EAMainController alloc] init];
            
            
            
            [self.navigationController pushViewController:mVC animated:YES];
            
            
            
        }
        
    }];
    
    
    
    
    
    
    
    

    
    
    
    




}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    
    

    
    
    
    switch (textField.tag) {


        case 2:{
            self.pickerArray=@[@"120cm",@"121cm",@"122cm",@"123cm",@"124cm",@"125cm",@"126cm",@"127cm",@"128cm",@"129cm",@"130cm",@"131cm",@"132cm",@"133cm",@"134cm",@"135cm",@"136cm",@"137cm",@"138cm",@"139cm",@"140cm",@"141cm",@"142cm",@"143cm",@"144cm",@"145cm",@"146cm",@"147cm",@"148cm",@"149cm",@"150cm",@"151cm",@"152cm",@"153cm",@"154cm",@"155cm",@"156cm",@"157cm",@"158cm",@"159cm",@"160cm",@"161cm",@"162cm",@"163cm",@"164cm",@"165cm",@"166cm",@"167cm",@"168cm",@"169cm",@"170cm",@"171cm",@"172cm",@"173cm",@"174cm",@"175cm",@"176cm",@"177cm",@"178cm",@"179cm",@"180cm",@"181cm",@"182cm",@"183cm",@"184cm",@"185cm",@"186cm",@"187cm",@"188cm",@"189cm",@"190cm",@"191cm",@"192cm",@"193cm",@"194cm",@"195cm",@"196cm",@"197cm",@"198cm",@"199cm",@"200cm"];
            
            
            
            
            textField.inputView=self.pinkerView;
            
            
            __weak typeof(self) weakSelf=self;
            
            
            self.pinkerView.okBlock=^(UIButton * bt)
            {
                
                textField.text=weakSelf.pickerArray[ [weakSelf.pinkerView.pickerView selectedRowInComponent:0]];
               
                [weakSelf cancelAllKey];
                
            };
            
            
            NSString * w=[NSString stringWithFormat:@"%@cm",ApplicationDelegate.userDictionary[@"height"]];
            
            @try {
                           NSInteger  iw=[self.pickerArray indexOfObject:w];
                   [self.pinkerView.pickerView selectRow:iw inComponent:0 animated:YES];
            }
            @catch (NSException *exception) {
                
            }
            @finally {
                
            }
            

            
         
            
        }
            break;
        case 3:
        {
self.pickerArray=@[@"30.0kg",@"30.5kg",@"31.0kg",@"31.5kg",@"32.0kg",@"32.5kg",@"33.0kg",@"33.5kg",@"34.0kg",@"34.5kg",@"35.0kg",@"35.5kg",@"36.0kg",@"36.5kg",@"37.0kg",@"37.5kg",@"38.0kg",@"38.5kg",@"39.0kg",@"39.5kg",@"40.0kg",@"40.0kg",@"40.5kg",@"41.0kg",@"41.5kg",@"42.0kg",@"42.5kg",@"43.0kg",@"43.5kg",@"44.0kg",@"44.5kg",@"45.0kg",@"45.5kg",@"46.0kg",@"46.5kg",@"47.0kg",@"47.5kg",@"48.0kg",@"48.5kg",@"49.0kg",@"49.5kg",@"50.0kg",@"50.0kg",@"50.5kg",@"51.0kg",@"51.5kg",@"52.0kg",@"52.5kg",@"53.0kg",@"53.5kg",@"54.0kg",@"54.5kg",@"55.0kg",@"55.5kg",@"56.0kg",@"56.5kg",@"57.0kg",@"57.5kg",@"58.0kg",@"58.5kg",@"59.0kg",@"59.5kg",@"60.0kg",@"60.0kg",@"60.5kg",@"61.0kg"];
            
            
            __weak typeof(self) weakSelf=self;
            textField.inputView=self.pinkerView;
   
            self.pinkerView.okBlock=^(UIButton * bt)
            {
                
                textField.text=weakSelf.pickerArray[ [weakSelf.pinkerView.pickerView selectedRowInComponent:0]];
                
                
                
                [weakSelf  cancelAllKey];
                
            };
            
            @try {
                NSString * w=[NSString stringWithFormat:@"%@kg",ApplicationDelegate.userDictionary[@"weight"]];
                NSInteger  iw=[self.pickerArray indexOfObject:w];
                
                [self.pinkerView.pickerView selectRow:iw inComponent:0 animated:YES];
            }
            @catch (NSException *exception) {
                
            }
            @finally {
                
            }
   
            
            
            
            
        }
            
            break;
            

        default:
            break;
    }
    
    [self.pinkerView.pickerView reloadAllComponents];
    
    UIView *subView=textField.superview;
    NSArray *arr=subView.subviews;
    
    
    
    
    for (UITableViewCell *cell in self.tableView.visibleCells) {
        
        UIView *contentView=cell.contentView;
        
        for (UIView * v in contentView.subviews) {
            
            if ([v respondsToSelector:@selector(setTextColor:)]) {
                
                [v setValue:[UIColor darkGrayColor] forKey:@"textColor"];
                
            }
            
            
            if ([v respondsToSelector:@selector(setImage:)]) {
                [v setValue:[UIImage imageNamed:@"xuanze1"] forKey:@"image"];
            }
            
            
            
            
        }
        
        
        for (UIView *v in arr) {
            
            if ([v respondsToSelector:@selector(setTextColor:)]) {
                
                [v setValue:kTingColor forKey:@"textColor"];
                
            }
            
            
            if ([v respondsToSelector:@selector(setImage:)]) {
                [v setValue:[UIImage imageNamed:@"xuanze2"] forKey:@"image"];
            }
            
            
            
        }
        
        
        
        
        
        
    }
    
    
    
    
    
    
    return YES;
    
    
    
    
    
    
}
-(EAPinKerView *)pinKerViews
{
    
    EAPinKerView * pinKerView=[[NSBundle mainBundle] loadNibNamed:@"EAPinKerView" owner:nil options:nil][0];
    
    pinKerView.pickerView.delegate=self;
    
    pinKerView.pickerView.dataSource=self;
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    return pinKerView;
    
    
}


-(void)cancelAllKey
{
    

    
    [self.weightTF resignFirstResponder];
    
    [self.heightTF resignFirstResponder];
    
    
    
}


#pragma mark----pinker--代理

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [self.pickerArray count];
}
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [self.pickerArray objectAtIndex:row];
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    
    
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    NSString *m=self.pickerArray[row];
    
    
    //    switch (pickerView.tag) {
    //        case 0:
    //            self.sexyTF.text=m;
    //            [self.sexyTF resignFirstResponder];
    //
    //            break;
    //        case 1:
    //    self.ageTF.text=m;
    //            [self.ageTF resignFirstResponder];
    //
    //            break;
    //        case 2:
    //            self.heightTF.text=m;
    //            [self.heightTF resignFirstResponder];
    //
    //            break;
    //        case 3:
    //            self.weightTF.text=m;
    //            [self.weightTF resignFirstResponder];
    //
    //            break;
    //            
    //        default:
    //            break;
    //    }
    
    
    
}

@end
